<template>
  <div class="yxp">
    <top-back :id="package_id" :title="title" type="1"></top-back>
    <div class="yxp-word-box">
    <div class="yxp-wordList-box">
      <div class="yxp-wordList-box-item">
        <div class="yxp-wordList-main">
          <div class="yxp-wordList-item yxp-wordLexicon-item" v-for="item in wordList" :key="item.id" @click="handleSelect(item)">
            <h3 class="title">{{item.level_title}}</h3>
            <p class="totalNum">共有{{item.total}}词</p>    	
        <span class="course_expire green" v-if="!item.is_expire">有效期：{{item.expire_time}}</span>
        <span class="course_expire red" v-else>有效期：{{item.expire_time}}</span>
          </div>
        </div>
      </div>
    </div>
    </div>
    <Empty v-if="isNone" />
    <div class="mark" v-if="isShow || isreFresh"></div>
    <div class="yxp-word-pop" v-if="isShow">
      <img src="@/assets/word/download-icon.png" class="downLoad"/>
      <p class="tips">正在导入单词包，请稍后（{{time}}s）…</p>
    </div>

    <div class="yxp-word-check" v-if="isreFresh">
      <img src="@/assets/word/word-check.png" class="check"/>
      <p class="tips font-20">{{detail}}</p>
      <div>
      <input type="password" class="ant-input" maxlength="12" id="login_pwd" name="login_pwd" placeholder="请输入登录密码" v-model="password">
      </div>
      <div class="btn">
        <div class="btn-item1" @click="handleOK" id="yxp-index-ok">确定</div>
        <div class="btn-item1" @click="handleCancle" id="yxp-index-cancle">取消</div>
      </div>
    </div>
      <Timetips ref="timetips"></Timetips>
  </div>
</template>
<script>
import TopBack from '@/components/TopBack.vue'
import Empty from '@/components/Empty.vue'
import { wordPackages, checkHasword ,checkisnow,refreshword} from '@/services/word.js'
import Timetips from "@/components/Timetips";
export default {
  name: 'wordLexicon',
  components: {
    TopBack,
    Empty,
    Timetips
  },
  data() {
    return {
      isShow: false,
      package_id: -1,
      wordList: [],
      title: '',
      time: 10,
      timer1: null,
      timer2: null,
      isNone: false,
      isEmpty: false,
      isreFresh: false,//是否重置学习记录
      detail:'',
      has_result:0,
      change_id:0,
      change_title:'',
      lastpackageid:'',
      lastpackagetitle:'',
      is_doubleminus:sessionStorage.getItem('doubleminus_type')
    }
  },
  created () {
    if (this.$route.query) {
      this.package_id = +this.$route.query.package_id
      this.title = this.$route.query.title
    }
    this.getWordPackages()
  },
  methods: {
    // 单词列表
    async getWordPackages () {
      this.$loading.show()
      let data = await wordPackages({})
      if (data.code === 1) {
        this.$loading.hide()
        if (data.data.length === 0) {
          this.isNone = true
          this.isEmpty = true
        }
        this.wordList = data.data
      } else if (data.code === '10001' || data.code === 10001) {
        this.$loading.hide()
        this.$toast(data.info)
      } else {
        this.$loading.hide()
        this.$toast(data.info)
      }
    },
    // 单词包导入
    async getWordCheck (id,title) {
      let data = await checkHasword({ package_id: id })
      if (data.code === 1) {
        if (data.data.type === 10001) {
          clearInterval(this.timer)
          clearInterval(this.timer2)
          this.isShow = false
          if(this.is_doubleminus == 2 || this.is_doubleminus == 3){
            this.checkisnow(id,title)
          }else {
            this.$router.push({
              path: '/wordRemember',
              query: {
                package_id: id,
                title: title
              }
            })
          }
        }
      } else if (data.code === '10001' || data.code === 10001) {
        // this.$loading.hide()
        this.$toast(data.info)
      } else {
        // this.$loading.hide()
        this.$toast(data.info)
      }
    },
	async export_words(_id,_title,has_result){
    if(has_result){
        this.$router.replace({
              path: '/wordRemember',
              query: {
              package_id: _id,
                title: _title
              }
            })
        }else {
         this.isShow = true
      this.getWordCheck(_id,_title)
      this.timer1 = setInterval(() => {
        this.time--
        if (this.time === 0) this.time = 10
      }, 1000)
      this.timer2 = setInterval(() => {
        this.getWordCheck(_id,_title)
      }, 5000)
    }
	},
    async checkisnow (_id,_level_title){
      let data = await checkisnow({ package_id: _id })
      if(data.code == 1){
        if(data.data.id > 0){
          //提示清空记录
          this.isreFresh = true
          this.detail = '您正在切换到新课程，继续操作将导致当前课程记录清零，请慎重！！！';
          this.lastpackageid = _id
          this.lastpackagetitle = _level_title
        }else{
          this.export_words(_id,_level_title,this.has_result)
        }
      }
    },
    //确定重置记录
    async handleOK (){
      //清空记录
      let _login_pwd = document.getElementById("login_pwd").value;
      let data = await refreshword({ pwd:_login_pwd })
      if(data.code == 1){
      this.export_words(this.lastpackageid,this.lastpackagetitle,this.has_result)
      }else{
        this.$toast(data.info)
      }
    },
    //不重置记录
    handleCancle (){
      this.isreFresh = false
    },
    // 选择
    handleSelect (item) {
    this.has_result = item.has_result
        if(this.is_doubleminus == 2 || this.is_doubleminus == 3){
          this.checkisnow(item.id,item.level_title)
        }else {
         this.export_words(item.id,item.level_title,item.has_result)
        }
    }
  },
  beforeDestroy() {
    this.$refs.timetips.stopTimer();
    this.$refs.timetips.stopEndTimer();
  }
}
</script>
<style lang="less" scoped>
.yxp-wordList-main {
  // display: flex;
  // justify-content: space-between;
  // flex-wrap: wrap;
  // cursor: pointer;
  .yxp-wordLexicon-item {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: flex-start;
    height: 160px;
    text-align: left;
    background: #fff;
    border-radius: 10px;
    margin-bottom: 20px;
    padding-left: 40px;
    padding-right: 40px;
    transition: box-shadow 0.3s;
    &:hover {
      box-shadow: 0px 0px 10px rgba(39, 191, 163, 0.5);
    }
    .title {
      font-size: 30px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      color: #333;
      // padding-top: 44px;
      // padding-left: 50px;
    }
    .totalNum {
      font-size: 20px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      padding-top: 7px;
      // padding-left: 50px;
    }
    .tag {
      position: absolute;
      right: 0;
      top: 0;
      width: 151px;
      height: 151px;
    }
  }
}
.font-20{
  font-size: 20px!important;
}
.yxp-word-check {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
  width: 330px;
  height: 398px;
  text-align: center;
  background: #fff;
  border-radius: 10px;
  padding-left: 15px;
  padding-right: 15px;
  z-index: 1000;
  .close {
    position: absolute;
    right: 20px;
    top: 20px;
    width: 20px;
    height: 20px;
    cursor: pointer;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .check {
    display: inline-block;
    width: 150px;
    height: 150px;
    margin: 25px auto 0;
  }
  .tips {
    font-size: 24px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #333333;
  }
  .btn {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 30px auto 0;
    &-item {
      width: 280px;
      height: 50px;
      line-height: 50px;
      text-align: center;
      font-size: 18px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #fff;
      background: #27BFA3;
      border-radius: 100px;
      cursor: pointer;
      &:hover {
        background: #18A38A;
      }
    }
    &-item1 {
      width: 130px;
      height: 50px;
      line-height: 50px;
      text-align: center;
      font-size: 18px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #fff;
      background: #27BFA3;
      border-radius: 100px;
      cursor: pointer;
      &:first-child {
        margin-right: 15px;
      }
      &:hover {
        background: #18A38A;
      }
    }
  }
}
.yxp-word-pop {
  position: fixed;
  left:50%;
  top: 50%;
  transform: translate(-55%,-50%);
  width: 600px;
  height: 400px;
  text-align: center;
  background: #fff;
  border-radius: 10px;
  z-index: 1200;
  .downLoad {
    display: block;
    width: 134px;
    height: 160px;
    margin: 94px auto 14px;
  }
  .tips {
    font-size: 24px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    line-height: 30px;
  }
}
</style>
